package com.laihui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.laihui.Constants;

/**
 * Created by chensk on 2017/12/7.
 */

public class TextProgress extends ProgressBar {
    String text;
    Paint mPaint;
    RectF mRectF;
    int progress = -1;
    public TextProgress(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mRectF = new RectF();
      //  System.out.println("1");
        initText();
    }


    public TextProgress(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
      //  System.out.println("2");
        mRectF = new RectF();
        initText();
    }


    public TextProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
       // System.out.println("3");
        mRectF = new RectF();


        mPaint = new Paint();
        initText();
    }

    @Override
    public synchronized void setProgress(int progress) {
        // TODO Auto-generated method stub
        setText(progress);

        super.setProgress(progress);
        this.progress = progress;
        invalidate();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        int width = this.getWidth();
        int height = this.getHeight();
        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        //this.setText();
        Rect rect = new Rect();
        // 位置
       //mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        //mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        //mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        //mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y

        int textWidth = (int) mPaint.measureText(text+"", 0, text.length());

        this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
        this.mPaint.setTextSize(Constants.dip2px(getContext(),9f));


        int x =  getProgress() *getWidth()/100-textWidth;

        int y = (getHeight() / 2) - rect.centerY();

        canvas.drawText(this.text, x, y, this.mPaint);
    }


    @Override
    public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
        super.setOnCreateContextMenuListener(l);
    }

    //初始化，画笔
    private void initText(){
        this.mPaint = new Paint();
        this.mPaint.setColor(Color.WHITE);

    }

    private void setText(){

        setText(this.getProgress());

    }

    //设置文字内容
    private void setText(int progress){
        int i = (progress * 100)/this.getMax();
        this.text = String.valueOf(i) + "%";
    }


}
